import datetime
import requests


class MySite:
    host = 'http://127.0.0.1:8888'
    client = None

    def __init__(self, url='/user/token-pair/', username='test01', password='test123456'):
        url = self.host + url
        form_data = {
            "username": username,
            "password": password,
        }

        client = requests.Session()
        response = client.post(url, form_data)
        if response.ok:
            data = response.json()
            access_token = "Bearer " + data['access']
            client.headers.update({"Authorization": access_token})
            self.client = client

    def get(self, url='', **kwargs):
        url = self.host + url
        return self.client.get(url, **kwargs)

    def post(self, url='', **kwargs):
        url = self.host + url
        return self.client.post(url, **kwargs)


def articles_list(client, url='/blog/articles/'):
    response = client.get(url)
    print(response.status_code)


def articles_create(client, url='/blog/articles/'):
    current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    form_data = {
        "title": f"标题 {current_time}",
        "content": f"内容{current_time}"
    }
    response = client.post(url, form_data)
    print(response.status_code)


if __name__ == '__main__':
    my_site = MySite()
    articles_list(my_site)
